#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;

int n = 0;
char stc;
vector<string> strs;
int ans = 0;
int add_depp[100][100];


void find_tail()
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            string temp1 = strs[i];
            string temp2 = strs[j];

            int p = 0, q = temp1.size() - 1, length = 1;
            bool judge = 0;
            while(length < min(temp1.size(), temp2.size()))
            {
                if(temp1.compare(q, length, temp2, p, length) == 0)
                {judge = 1; break;}
                else
                    q--, length += 1;
            }
            if(judge)   add_depp[i][j] = temp2.size() - length;
        }
    }
};

int been[30] = {0};

struct Node
{
    vector<Node> sons;
    int type = -1;
    int length = 0;
};


void dfs(Node now)
{
    been[now.type] += 1;

    for(int i = 0; i < strs.size(); i++)
    {
        if(been[i] >= 2 || add_depp[now.type][i] == -1)
            continue;
        else
        {
            Node temp;
            temp.type = i;
            temp.length = now.length + add_depp[now.type][i];
            now.sons.push_back(temp);
        }
    }

    if(now.sons.empty())
    {    if(ans < now.length)    ans = now.length;}
    else
    {
        for(int i = 0 ; i < now.sons.size(); i++)
            dfs(now.sons[i]);
    }

    been[now.type] -= 1;
}

int main()
{
    memset(add_depp, -1, sizeof(add_depp));
    cin >> n;
    for(int i = 0 ; i < n; i++)
    {
        string temp;
        cin >> temp;
        strs.push_back(temp);
    }

    cin >> stc;

    find_tail();

    Node now;    
    for(int i = 0; i < strs.size(); i++)
    {
        if(strs[i][0] == stc)
        {
            Node temp;
            temp.type = i;
            temp.length = strs[i].size();
            now.sons.push_back(temp);
        }
    }

    if(!now.sons.empty())
    {
        for(int i = 0; i < now.sons.size(); i++)
        {
            dfs(now.sons[i]);
        }
    }
    
    cout << ans;
    return 0;
}